package com.atguigu.gmall.all.controller;

import com.alibaba.csp.sentinel.util.StringUtil;
import com.atguigu.gmall.list.client.ListFeignClient;
import com.atguigu.gmall.model.list.SearchParam;
import com.atguigu.gmall.model.list.SearchResponseVo;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import sun.swing.StringUIClientPropertyKey;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Controller
public class ListController {

    @Resource
    private ListFeignClient listFeignClient;

    @GetMapping("/list")
    public String list(/*@RequestParam*/ SearchParam searchParam, Model model){
        //回显页面  关键词
        model.addAttribute("searchParam",searchParam);
        SearchResponseVo responseVo = listFeignClient.search(searchParam);
        //品牌集合
        model.addAttribute("trademarkList",responseVo.getTrademarkList());
        //URLparam
        String urlParam = buildUrlParam(searchParam);
        model.addAttribute("urlParam",urlParam);
        //平台属性
        model.addAttribute("attrsList",responseVo.getAttrsList());
        //排序回显
        Map orderMap = buildOrderMap(searchParam);
        model.addAttribute("orderMap",orderMap);
        //结果集
        model.addAttribute("goodsList",responseVo.getGoodsList());
        //当前页
        model.addAttribute("pageNo",responseVo.getPageNo());
        //总页数
        model.addAttribute("totalPages",responseVo.getTotalPages());
        //选择品牌之后回显已选择结果     筛选后可×的框
        String trademarkParam = buildTrademarkParam(searchParam);
        model.addAttribute("trademarkParam",trademarkParam);
        //选择平台属性之后回显已选择平台属性结果
        List<Map> propsParamList = buildPropsParamList(searchParam);
        model.addAttribute("propsParamList",propsParamList);

        return "list/index";
    }

    //选择平台属性之后回显已选择平台属性结果
    private List<Map> buildPropsParamList(SearchParam searchParam) {
        String[] props = searchParam.getProps();//props=23:4G:运行内存
        if (props!=null && props.length > 0){
            return Arrays.stream(props).map(prop->{
                String[] split = prop.split(":");
                Map<Object, Object> map = new HashMap<>();
                map.put("attrId",split[0]);
                map.put("attrName",split[2]);
                map.put("attrValue",split[1]);
                return map;
            }).collect(Collectors.toList());
        }
        return null;
    }

    //选择品牌之后回显已选择结果     筛选后可×的框
    private String buildTrademarkParam(SearchParam searchParam) {
        String trademark = searchParam.getTrademark();//2：华为
        if (!StringUtils.isEmpty(trademark)){
            String[] split = trademark.split(":");
            return "品牌：" + split[1];
        }
        return null;
    }

    //排序回显
    private Map buildOrderMap(SearchParam searchParam) {
        Map<Object, Object> map = new HashMap<>();
        //获取当前排序
        String order = searchParam.getOrder();
        if (!StringUtils.isEmpty(order)){
            String[] split = order.split(":");
            map.put("type",split[0]);
            map.put("sort",split[1]);
        }else {//若空，给默认值
            map.put("type","1");
            map.put("sort","asc");
        }
        return map;
    }

    //URLparam
    private String buildUrlParam(SearchParam searchParam) {
        //stringbuilder&stringbuffer    改变原有字符串，不新建
        StringBuilder sb = new StringBuilder();//性能高、线程不安全的字符串缓冲区
        String keyword = searchParam.getKeyword();
        if (!StringUtil.isEmpty(keyword)){
            sb.append("/list?keyword=").append(searchParam.getKeyword());//拼接
        }
        String trademark = searchParam.getTrademark();
        if (!StringUtils.isEmpty(trademark)) {
            sb.append("&trademark=").append(searchParam.getTrademark());
        }
        String[] props = searchParam.getProps();
        if (null != props && props.length > 0) {
            for (String prop : props) {
                sb.append("&props=").append(prop);
            }
        }
        return "/list?" + sb.toString();
    }
}
